package com.vates.vsp.practico.commons;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class MailValidator implements Validator {

	private Pattern pattern;
	private Matcher matcher;

	public MailValidator() {
		pattern = Pattern.compile(".+@.+\\.[a-z]+");
	}
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		matcher = pattern.matcher(value.toString());
		if (!matcher.matches()) {
			FacesMessage msg = new FacesMessage();
			msg.setSummary("Email not valid.");
			throw new ValidatorException(msg);
		}
	}
}
